#/bin/bash
#将指定文件实时上传到obs上

inotify_cp_obs(){
object=xxx
project=xxx
project_url=/app/project/xxx
project_pwd="resource/ poster/ user/ item/ content/ attachment/ static/  web/ certificate/ wb_image/"
project_file=".ts|.mp4|.mp3|.mov|.png|.gif|.jpg|.pdf|.pptx|.ppt|.xlsx|.xls|.docx|.doc|.epub|.apk"
cd $project_url

#新建文件上传到obs上
/usr/local/bin/inotifywait -mrq $project_pwd   --format '%w%f' -e  create | while read files;
do
    #指定文件上传到obs上
    if [[  `echo  $files | grep -v temp | grep -E "$project_file" | wc -l`  == 1   ]];then
           filesize=`ls -lh $project_url/$files | awk '{print $5}'`
           datetime=`date +%F-%H:%M:%S`
           #防止视频文件上传不完整
           if [[ `echo  $files |  grep mp4 | wc -l`  == 1  ]];then
              sleep 2
           fi 
           filesize=`ls -lh $project_url/$files | awk '{print $5}'`
           datetime=`date +%F-%H:%M:%S`
           echo  upload,$datetime,$files >>/app/shell/log/tmp_obs.log
           /usr/local/bin/obsutil  cp   $files obs://$object/$project/$files

    fi  
done
}

inotify_cp_obs
